package global

import (
	"fmt"
	"gin-proxy/common/errdef"
	"gin-proxy/common/response"

	"github.com/gin-gonic/gin"
)

func ErrorHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				if bizErr, ok := err.(errdef.BizErr); ok {
					response.Fail(c, bizErr)
				} else {
					response.Fail(c, errdef.NewServerErr(fmt.Sprintf("%v", err)))
				}
			}
		}()
		c.Next() // 继续执行后续的处理器
	}
}
